package com.yeqing.crazymakercircle.bufferDemo;

import java.nio.IntBuffer;

public class UseBuffer {
    public static void allocateTest() {
        //创建了一个Intbuffer实例对象
        IntBuffer intBuffer = IntBuffer.allocate(20);
        System.out.println("------------after allocate------------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
    }

    public static void putTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);

        for (int i = 0; i < 5; i++) {
            //写入一个整数到缓冲区
            intBuffer.put(i);
        }

        //输出缓冲区的主要属性值
        System.out.println("------------after putTest------------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
    }

    public static void flipTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);

        for (int i = 0; i < 5; i++) {
            //写入一个整数到缓冲区
            intBuffer.put(i);
        }

        //输出缓冲区的主要属性值
        System.out.println("------------after putTest------------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
        //翻转缓冲区，从写入模式翻转成读取模式
        intBuffer.flip();
        //输出缓冲区的主要属性值
        System.out.println("------------after flip ------------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
    }

    public static void getTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);
        for (int i = 0; i < 5; i++) {
            //写入一个整数到缓冲区
            intBuffer.put(i);
        }
        //翻转缓冲区，从写入模式翻转成读取模式
        intBuffer.flip();
        //先读2个数据

        for (int i = 0; i < 2; i++) {
            int j = intBuffer.get();
            System.out.println("j = " + j);
        }

        // 输出缓冲区的主要属性值
        System.out.println("---------after get 2 int --------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());

        // 再读3个数据
        for (int i = 0; i < 3; i++) {
            int j = intBuffer.get();
            System.out.println("j = " + j);
        }

        // 输出缓冲区的主要属性值
        System.out.println("---------after get 3 int ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
    }


    public static void rewindTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);
        for (int i = 0; i < 5; i++) {
            //写入一个整数到缓冲区
            intBuffer.put(i);
        }
        //翻转缓冲区，从写入模式翻转成读取模式
        intBuffer.flip();
        //先读2个数据

        for (int i = 0; i < 2; i++) {
            int j = intBuffer.get();
            System.out.println("j = " + j);
        }

        // 输出缓冲区的主要属性值
        System.out.println("---------after get 2 int --------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());

        // 再读3个数据
        for (int i = 0; i < 3; i++) {
            int j = intBuffer.get();
            System.out.println("j = " + j);
        }

        // 输出缓冲区的主要属性值
        System.out.println("---------after get 3 int ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());

        // 倒带
        intBuffer.rewind();

        // 输出缓冲区属性
        System.out.println("------------after rewind ------------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
    }


    public static void reRead() {
        IntBuffer intBuffer = IntBuffer.allocate(20);
        for (int i = 0; i < 5; i++) {
            //写入一个整数到缓冲区
            intBuffer.put(i);
        }
        //翻转缓冲区，从写入模式翻转成读取模式
        intBuffer.flip();

        // 读5个数据
        for (int i = 0; i < 5; i++) {
            int j = intBuffer.get();
            System.out.println("j = " + j);
        }

        // 输出缓冲区的主要属性值
        System.out.println("---------after get 5 int ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());

        // 倒带
        intBuffer.rewind();

        // 输出缓冲区属性
        System.out.println("------------after rewind ------------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());

        // 读5个数据
        for (int i = 0; i < 5; i++) {
            int j = intBuffer.get();
            System.out.println("j = " + j);
        }

        // 输出缓冲区的主要属性值
        System.out.println("---------after get 5 int ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
    }

    public static void afterReset() {
        IntBuffer intBuffer = IntBuffer.allocate(20);
        for (int i = 0; i < 5; i++) {
            //写入一个整数到缓冲区
            intBuffer.put(i);
        }
        //翻转缓冲区，从写入模式翻转成读取模式
        intBuffer.flip();

        // 读5个数据
        for (int i = 0; i < 5; i++) {
            if (i == 2) {
                intBuffer.mark();
            }
            int j = intBuffer.get();
        }

        // 输出缓冲区的主要属性值
        System.out.println("---------after get 5 int ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());

        intBuffer.reset();

        System.out.println("---------after reset int ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());

        for (int i = 2; i < 5; i++) {
            int j = intBuffer.get();
        }

        System.out.println("---------after get 3 int ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
    }
    
    public static void claerTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);
        for (int i = 0; i < 5; i++) {
            //写入一个整数到缓冲区
            intBuffer.put(i);
        }
        //翻转缓冲区，从写入模式翻转成读取模式
        intBuffer.flip();

        // 读5个数据
        for (int i = 0; i < 5; i++) {
            intBuffer.get();
        }
        System.out.println("---------after get 5 int ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());

        // 清空缓冲区，进入写入模式
        intBuffer.clear();

        // 输出缓冲区的属性值
        System.out.println("---------after clear ---------------");
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        System.out.println("capacity=" + intBuffer.capacity());
    }


    public static void main(String[] args) {
//        allocateTest();
//        putTest();
//        flipTest();
//        getTest();
//        rewindTest();
//        reRead();
//        afterReset();
        claerTest();
    }
}